package com.imooc.miaosha.controller;

import com.imooc.miaosha.domian.OrderInfo;
import com.imooc.miaosha.domian.User;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.result.ResultEnums;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.vo.GoodsVO;
import com.imooc.miaosha.vo.OrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 模块描述: <br>
 * (订单控制器)
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/23 17:27
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    /**
     * 查询订单信息，返回到订单详情页面
     *
     * @param user
     * @param orderId
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public Result<OrderDetailVO> info(User user, @RequestParam("orderId") long orderId) {
        if (user == null) {
            return Result.error(ResultEnums.SESSION_ERROR);
        }
        OrderInfo orderInfo = orderService.getOrderById(orderId);
        if (orderInfo == null) {
            return Result.error(ResultEnums.ORDER_NOT_EXIST);
        }
        Long goodsId = orderInfo.getGoodsId();
        GoodsVO goodsVO = goodsService.getGoodsVoByGoodsId(goodsId);
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setGoodsVO(goodsVO);
        orderDetailVO.setOrderInfo(orderInfo);
        return Result.success(orderDetailVO);
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

}
